package com.jfinal.demo.web.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;

/**
 * session拦截器。会话管理
 * 
 * @author Administrator
 * 
 */
public class SessionInterceptor implements Interceptor {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void intercept(ActionInvocation ai) {
		String actionKey = ai.getActionKey();
		logger.debug(actionKey);
		if (!("/".equals(actionKey) || "/login".equals(actionKey) || "logout"
				.equals(actionKey) || "/captcha".equals(actionKey))
				&& ai.getController().getSession().getAttribute("user") == null) {
			ai.getController().redirect("/");
		} else {
			ai.invoke();
		}
	}

}
